package model 
{
	import controller.EventBus;
	import controller.GameModelEvent;
	import controller.GameViewControllerEvent;
	import controller.NPCModelEvent;
	import controller.TimerModelEvent;
	/**
	 * ...
	 * @author Gaz Williams
	 */
	public class GameTimerModel 
	{
		
		private var wrongPenalty:uint;
		private var rightBonus:uint;
		
		private var bus:EventBus;
		private var timeRemaining:uint;
		
		public function GameTimerModel(initialTime:uint, wrongPenalty:uint, rightBonus:uint):void
		{
			
			this.wrongPenalty = wrongPenalty;
			this.rightBonus = rightBonus;
			
			bus = EventBus.getInstance();
			bus.addEventListener(GameViewControllerEvent.UPDATE, handleUpdate);
			bus.addEventListener(NPCModelEvent.RIGHT_ITEM, handleRightItemGiven);
			bus.addEventListener(NPCModelEvent.WRONG_ITEM, handleWrongItemGiven);
			
			timeRemaining = initialTime;
		}
		
		private function handleWrongItemGiven(e:NPCModelEvent):void 
		{
			timeRemaining -= wrongPenalty;
		}
		
		private function handleRightItemGiven(e:NPCModelEvent):void 
		{
			timeRemaining += rightBonus;
		}
		
		public function dispose():void 
		{
			bus.removeEventListener(GameViewControllerEvent.UPDATE, handleUpdate);
		}
		
		private function handleUpdate(e:GameViewControllerEvent):void 
		{
			timeRemaining--;
			
			bus.dispatchEvent(new TimerModelEvent(TimerModelEvent.TIME_UPDATED, timeRemaining));
		}
		
	}

}